package com.zheng.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/4/20
 * Time: 16:42
 */
public class TimeUtil {
    private static final int DEFAULT_DAY = 1;
    private static final int DEFAULT_MONTH = 1;

    public static int getSundayOfMonth(Date beginTime) {
        Instant instant = beginTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate today = instant.atZone(zoneId).toLocalDate();
        return getSundayOfMonth(today);
    }

    public static int getSundayOfMonth(LocalDate today) {
        int weeks = 6;
        int i = 1;
        Month month = today.getMonth();
        //本月每周日的日期
        //本月的第一天
        LocalDate firstday = LocalDate.of(today.getYear(), today.getMonth(), 1);
        LocalDate weekDate = firstday.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        while (i <= weeks) {
            weekDate = weekDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
            if (weekDate.getMonthValue() > month.getValue()) {
                break;
            }
            i++;
        }
        return i;
    }


    public static void test() {
        LocalDate now = LocalDate.of(2018, 4, 8);
        int sundayCount = 0;
        int dayCount = now.lengthOfMonth();
        for (int i = now.getDayOfMonth(); i <= dayCount; i++) {
            now = now.withDayOfMonth(i);
            DayOfWeek dayOfWeek = now.getDayOfWeek();
            if (dayOfWeek.getValue() == 7) {
                sundayCount++;
            }
        }
        System.out.println(sundayCount);

    }

    public static void main(String[] args) {
        int sundayOfMonth = getSundayOfMonth(new Date());
        System.out.println(sundayOfMonth);
    }

    //获取某周在当月是第几周
    public static int getWeekOfMonth(LocalDate date) throws ParseException {
        String dateStr = date.toString();
        Date now = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            now = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        return calendar.get(Calendar.WEEK_OF_MONTH);
    }

    //获取某月共几周
    public static int getWeekCount(LocalDate date) {
        Calendar instance = Calendar.getInstance();
        return instance.getActualMaximum(Calendar.WEEK_OF_MONTH);
    }

    public static Date concatDate(Integer year, Integer month, Integer day) throws Exception {
        String dateStr = year + "-" + month + "-" + day;
        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
        return smf.parse(dateStr);
    }

    public static Date concatDate(Integer year, Integer month) throws Exception {
        return concatDate(year, month, DEFAULT_DAY);
    }

    public static Date concatDate(Integer year) throws Exception {
        return concatDate(year, DEFAULT_MONTH, DEFAULT_DAY);
    }
}
